package cn.ixinjiu.libraryMangement.dao.impl;

import cn.ixinjiu.libraryMangement.dao.IBorrowInfoDao;
import cn.ixinjiu.libraryMangement.entity.BorrowInfo;
import cn.ixinjiu.libraryMangement.utils.JDBCUtil;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XinChen on 2023-05-12
 *
 * @TODO
 */
public class BorrowInfoDaoImpl implements IBorrowInfoDao {
    @Override
    public List<BorrowInfo> borrowsInfo() {
        try {
            ResultSet query = JDBCUtil.query("select * from borrow_info", null);
            ArrayList<BorrowInfo> borrowInfos = new ArrayList<>();
            while (query.next()) {
                int id = query.getInt("id");
                int userId = query.getInt("user_id");
                int bookId = query.getInt("book_id");
                Date borrowDate = query.getDate("borrow_date");
                Date returnDate = query.getDate("return_date");
                Date actualReturn = query.getDate("actual_return");
                int isReturned = query.getInt("is_returned");
                int isOverdue = query.getInt("is_overdue");
                int overdueDays = query.getInt("overdue_days");
                BorrowInfo borrowInfo = new BorrowInfo(id, userId, bookId, borrowDate, returnDate, actualReturn, isReturned, isOverdue, overdueDays);
                borrowInfos.add(borrowInfo);
            }
            return borrowInfos;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close();
        }
    }

    @Override
    public List<BorrowInfo> getBorrowsInfoByUserId(int userid) {
        try {
            ResultSet query = JDBCUtil.query("select * from borrow_info where user_id = ?", new Object[]{userid});
            ArrayList<BorrowInfo> borrowInfos = new ArrayList<>();
            while (query.next()) {
                int id = query.getInt("id");
                int userId = query.getInt("user_id");
                int bookId = query.getInt("book_id");
                Date borrowDate = query.getDate("borrow_date");
                Date returnDate = query.getDate("return_date");
                Date actualReturn = query.getDate("actual_return");
                int isReturned = query.getInt("is_returned");
                int isOverdue = query.getInt("is_overdue");
                int overdueDays = query.getInt("overdue_days");
                BorrowInfo borrowInfo = new BorrowInfo(id, userId, bookId, borrowDate, returnDate, actualReturn, isReturned, isOverdue, overdueDays);
                borrowInfos.add(borrowInfo);
            }
            return borrowInfos;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close();
        }
    }

    @Override
    public int count() {
        try {
            ResultSet query = JDBCUtil.query("SELECT COUNT(*) as count FROM `borrow_info`", null);
            int count = 0;
            while (query.next()) {
                count = query.getInt("count");
            }
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close();
        }
    }

    @Override
    public int borrowInfoDel(int id) {
        return JDBCUtil.update("delete from borrow_info where id = ?", new Object[]{id});
    }

    @Override
    public int borrowInfoDelByBookId(int bookId) {
        return JDBCUtil.update("delete from borrow_info where book_id = ?", new Object[]{bookId});
    }

    @Override
    public int borrowUpd(BorrowInfo borrowInfo) {
        // UPDATE   table_name SET column1 = value1, column2 = value2, ...   WHERE condition;
        String sql = "UPDATE `library`.`borrow_info` SET `user_id` = ?, `book_id` = ?, `borrow_date` = ?, " +
                "`return_date` = ?, `actual_return` = ?, `is_returned` = ?, `is_overdue` = ?, " +
                "`overdue_days` = ? WHERE `id`= ?;";
        Object[] objects = {borrowInfo.getUserId(), borrowInfo.getBookId(), borrowInfo.getBorrowDate(),
                borrowInfo.getReturnDate(), borrowInfo.getActualReturn(), borrowInfo.getIsReturned(),
                borrowInfo.getIsOverdue(), borrowInfo.getOverdueDays(), borrowInfo.getId()};
        return JDBCUtil.update(sql, objects);
    }

    @Override
    public int borrowAdd(BorrowInfo borrowInfo) {
        String sql = "insert into borrow_info values(null, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] objects = {borrowInfo.getUserId(), borrowInfo.getBookId(), borrowInfo.getBorrowDate(),
                borrowInfo.getReturnDate(), borrowInfo.getActualReturn(), borrowInfo.getIsReturned(),
                borrowInfo.getIsOverdue(), borrowInfo.getOverdueDays()};
        return JDBCUtil.update(sql, objects);
    }

    @Override
    public BorrowInfo getBorrowInfoById(int bid) throws SQLException {
        ResultSet query = JDBCUtil.query("select * from borrow_info where id = ?", new Object[]{bid});
        while (query.next()) {
            int id = query.getInt("id");
            int userId = query.getInt("user_id");
            int bookId = query.getInt("book_id");
            Date borrowDate = query.getDate("borrow_date");
            Date returnDate = query.getDate("return_date");
            Date actualReturn = query.getDate("actual_return");
            int isReturned = query.getInt("is_returned");
            int isOverdue = query.getInt("is_overdue");
            int overdueDays = query.getInt("overdue_days");
            return new BorrowInfo(id, userId, bookId, borrowDate, returnDate, actualReturn, isReturned, isOverdue, overdueDays);
        }
        return null;
    }

    @Override
    public BorrowInfo getBorrowInfoByBookId(int bbookId) {
        try {
            ResultSet query = JDBCUtil.query("select * from borrow_info where book_id = ?", new Object[]{bbookId});
            while (query.next()) {
                int id = query.getInt("id");
                int userId = query.getInt("user_id");
                int bookId = query.getInt("book_id");
                Date borrowDate = query.getDate("borrow_date");
                Date returnDate = query.getDate("return_date");
                Date actualReturn = query.getDate("actual_return");
                int isReturned = query.getInt("is_returned");
                int isOverdue = query.getInt("is_overdue");
                int overdueDays = query.getInt("overdue_days");
                return new BorrowInfo(id, userId, bookId, borrowDate, returnDate, actualReturn, isReturned, isOverdue, overdueDays);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
